#include "sched.h"
#include "screen.h"
#include "stdio.h"
#include "syscall.h"
#include "time.h"

#include "test4.h"

#define RW_TIMES 2
int pro_atoi(char *input)
{
	int value = 0;
	int i;
	if(input[0] == '0' && input[1] == 'x'){
		for(i = 2; ;i++){
			if(input[i] >= '0' && input[i] <= '9'){
				value = value * 16 + input[i] - '0';
			}
			else if(input[i] >= 'a' && input[i] <= 'f'){
				value = value * 16 + (input[i] - 'a' + 10);
			}
			else if(input[i] == '\0'){
				break;
			}
			else{
				printf("wrong input:%s at %d\n", input, i);
			}
		}
	}
	return value;
}

int rand()
{
	int num = get_cp0_count();
	return num % 10000;
}

void rw_task1(int argc, char (*argv)[20])
{
	// printf("----test-----\n%s\n", argv[2]);
	// printf("%s\n", argv[3]);
	// printf("%s\n", argv[4]);
	// printf("%s\n----test----\n", argv[5]);
	int mem1, mem2 = 0;
	int curs = 0;
	int memory[RW_TIMES * 2];

	int i = 0;

	//srand((uint32_t)get_ticks());
	for (i = 0; i < RW_TIMES; i++)
	{
		sys_move_cursor(1, curs + i);
		mem1 = pro_atoi(argv[i + 2]);

		memory[i] = mem2 = rand();
		*(int *)mem1 = mem2;
		printf("Write: 0x%x,%d\n", mem1, mem2);
	}
	curs = RW_TIMES;
	for (i = 0; i < RW_TIMES; i++)
	{
		sys_move_cursor(1, curs + i);
		mem1 = pro_atoi(argv[RW_TIMES + i + 2]);

		memory[i + RW_TIMES] = *(int *)mem1;
		if (memory[i + RW_TIMES] == memory[i])
			printf("Read succeed: 0x%x,%d\n", mem1, memory[i + RW_TIMES]);
		else
			printf("Read error: 0x%x,%d\n", mem1, memory[i + RW_TIMES]);
	}
	sys_exit();
}
